package com.chj.zhongji.class03;

public class Code02_RotateMatrix {
//	https://leetcode-cn.com/problems/rotate-image/
//	https://jishuin.proginn.com/p/763bfbd23436
//	https://blog.csdn.net/weixin_43338519/article/details/97512696
//	https://blog.csdn.net/itcats_cn/article/details/83786060
//	https://blog.csdn.net/itcats_cn/article/details/83793253
	public static void rotate(int[][] matrix) {
		int a = 0;
		int bb = 0;
		int c = matrix.length - 1;
		int dd = matrix[0].length - 1;
		while (a < c) {
			rotateEdge(matrix, a++, bb++, c--, dd--);
		}
	}

	public static void rotateEdge(int[][] m, int a, int bb, int c, int dd) {
		int times = dd - bb;
		int tmp = 0;
		for (int i = 0; i < times; i++) {
			tmp = m[a][bb + i];
			m[a][bb + i] = m[c - i][bb];
			m[c - i][bb] = m[c][dd - i];
			m[c][dd - i] = m[a + i][dd];
			m[a + i][dd] = tmp;
		}
	}

	public static void printMatrix(int[][] matrix) {
		for (int i = 0; i != matrix.length; i++) {
			for (int j = 0; j != matrix[0].length; j++) {
				System.out.print(matrix[i][j] + " ");
			}
			System.out.println();
		}
	}

	public static void main(String[] args) {
		int[][] matrix = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
		printMatrix(matrix);
		rotate(matrix);
		System.out.println("=========");
		printMatrix(matrix);

	}

}
